﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;

using AtletIS.Shared.Entities;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.WpfControls
{
    public partial class StartList : UserControl
    {
        private int PAGE_SIZE = Properties.Settings.Default.PAGE_SIZE;
        private int pageIndex = 0;
        private int pages = 1;

        private System.Windows.Forms.Timer tmrRefresh;
        private DisciplineListEntity discipline;
        private DataTable dtPerformances;
        private DataRow[] aPerformances;

        // Constructor
        public StartList(DisciplineListEntity discipline)
        {
            InitializeComponent();
            this.discipline = discipline;

            cbTitle.title1 = discipline.Name;
            cbTitle.title2 = "Start List";
        }
        
        // Form Load
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            string sSort;

            try
            {
                this.dtPerformances = DatabaseHelper.GetPerformancesByDisciplineListId(discipline.ID); 
                // taPerformances.GetByDisciplineListId(discipline.ID);
                
                // Sort mode
                switch (discipline.DisciplineCode)
                {
                    case Globals.SprintCode:
                    case Globals.LongRunCode:
                        foreach (DataRow dr in this.dtPerformances.Rows)
                        {
                            if (Convert.ToInt32(dr["SB"]) == 0)
                                dr["SB"] = int.MaxValue;

                            if (Convert.ToInt32(dr["PB"]) == 0)
                                dr["PB"] = int.MaxValue;
                        }    
                    sSort = "Lane, SB DESC, PB DESC, StartNumber, Surname";
                        break;

                    case Globals.MarathonCode:
                        sSort = "StartNumber, Surname";
                        break;

                    default:
                        sSort = "Lane, SB ASC, PB ASC, StartNumber, Surname";
                        break;
                }

                this.aPerformances = dtPerformances.Select(null, sSort);
                
                // Pages
                if (this.aPerformances.Length % PAGE_SIZE == 0)
                    this.pages = this.aPerformances.Length / PAGE_SIZE;
                else
                    this.pages = this.aPerformances.Length / PAGE_SIZE + 1;

                // Timer
                this.tmrRefresh = new System.Windows.Forms.Timer();
                this.tmrRefresh.Interval = 1000;
                this.tmrRefresh.Tick += new System.EventHandler(this.tmrRefresh_Tick);
                this.tmrRefresh.Enabled = true;
                this.FillListView();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Nastala chyba pri generovaní štartovnej listiny: " + ex.Message,
                    "Štartovná listina", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        // Timer Tick
        private void tmrRefresh_Tick(object sender, EventArgs e)
        {
            this.FillListView();
        }

        // Fill ListView
        private void FillListView()
        {
            string sSB, sPB;
            this.lbStartList.Items.Clear();
            
            // Print
            for (int i = this.pageIndex * PAGE_SIZE; i < this.pageIndex * PAGE_SIZE + PAGE_SIZE; i++)
            {
                if (i < this.aPerformances.Length)
                {
                    sSB = string.Empty;
                    if (Convert.ToInt32(aPerformances[i]["SB"]) != 0 & Convert.ToInt32(aPerformances[i]["SB"]) != int.MaxValue)
                        sSB = Helpers.PrintHelper.PrintResult(aPerformances[i]["SB"].ToString(), discipline.DisciplineCode);

                    sPB = string.Empty;
                    if (Convert.ToInt32(aPerformances[i]["PB"]) != 0 & Convert.ToInt32(aPerformances[i]["PB"]) != int.MaxValue)
                        sPB = Helpers.PrintHelper.PrintResult(aPerformances[i]["PB"].ToString(), discipline.DisciplineCode);

                    StartListBox grb = new StartListBox(Convert.ToInt32(aPerformances[i]["StartNumber"]),
                        Convert.ToInt32(aPerformances[i]["Lane"]),
                        aPerformances[i]["CountryId"].ToString(),
                        aPerformances[i]["Surname"].ToString().ToUpper() + " " + aPerformances[i]["FirstName"].ToString(),
                        sSB,
                        sPB);

                    this.lbStartList.Items.Add(grb);
                }
            }

            this.footer.txtPage.Text = (this.pageIndex + 1).ToString() + " / " + this.pages.ToString();
        }

        #region Prev / Next

        // Left / Right Key
        private void UserControl_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (this.pages > 1)
                {
                    if (e.Key == Key.Right)
                    {
                        this.pageIndex += 1;

                        if (this.pageIndex >= this.pages)
                            this.pageIndex = this.pages - 1;

                        this.FillListView();
                    }

                    if (e.Key == Key.Left)
                    {
                        this.pageIndex -= 1;

                        if (this.pageIndex < 0)
                            this.pageIndex = 0;

                        this.FillListView();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Štartovná listina", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (this.pages > 1)
            {
                this.pageIndex += 1;

                if (this.pageIndex >= this.pages)
                    this.pageIndex = this.pages - 1;

                this.FillListView();
            }
        }

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            if (this.pages > 1)
            {
                this.pageIndex -= 1;

                if (this.pageIndex < 0)
                    this.pageIndex = 0;

                this.FillListView();
            }
        }

        #endregion



    }
}
